/**
 * 
 */
package org.seckill.util.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 
 * @author yue
 * 2016年6月1日 下午11:29:40
 */
public  class DynamicProxyUtil implements InvocationHandler {
	
	private Object proxy;
	private CutHandler cut;
	
	public DynamicProxyUtil(Object proxy,CutHandler cut){
		this.proxy = proxy;
		this.cut = cut;
	}
	
	/**
	 * @param <T>
	 * @return 
	 * @return 
	 * @return proxyed
	 */
	public static Object getProxy(Object obj,CutHandler cut) {
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new DynamicProxyUtil(obj,cut));
	}
	
	/* 
	 * @param proxy
	 * @param method
	 * @param args
	 * @return
	 * @throws Throwable 
	 */
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		cut.invokeBefore(method, args);
		Object result = method.invoke(this.proxy, args);
		cut.invokeAfter(method, result);
		return result;
	}

}
